package com.yuyang.gulimall.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuyang.common.utils.PageUtils;
import com.yuyang.common.utils.Query;
import com.yuyang.common.utils.R;
import com.yuyang.gulimall.product.config.MyThreadPoolConfig;
import com.yuyang.gulimall.product.dao.AttrGroupDao;
import com.yuyang.gulimall.product.dao.SkuInfoDao;
import com.yuyang.gulimall.product.dao.SkuSaleAttrValueDao;
import com.yuyang.gulimall.product.entity.SkuImagesEntity;
import com.yuyang.gulimall.product.entity.SkuInfoEntity;
import com.yuyang.gulimall.product.entity.SpuInfoDescEntity;
import com.yuyang.gulimall.product.feign.SeckillFeignService;
import com.yuyang.gulimall.product.service.SkuImagesService;
import com.yuyang.gulimall.product.service.SkuInfoService;
import com.yuyang.gulimall.product.service.SkuSaleAttrValueService;
import com.yuyang.gulimall.product.service.SpuInfoDescService;
import com.yuyang.gulimall.product.vo.SeckillSkuVo;
import com.yuyang.gulimall.product.vo.SkuItemSaleAttrVo;
import com.yuyang.gulimall.product.vo.SkuItemVo;
import com.yuyang.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    SkuSaleAttrValueDao skuSaleAttrValueDao;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    SeckillFeignService seckillFeignService;
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        String brandId = (String) params.get("brandId");
        String catelogId = (String) params.get("catelogId");
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        lambdaQueryWrapper.and(StringUtils.isNotBlank(key), wrapper -> wrapper.eq(SkuInfoEntity::getSkuId, key).or().like(SkuInfoEntity::getSkuName, key)).
                eq(StringUtils.isNotBlank(brandId) && !"0".equals(brandId), SkuInfoEntity::getBrandId, brandId).
                eq(StringUtils.isNotBlank(catelogId) && !"0".equals(catelogId), SkuInfoEntity::getCatalogId, catelogId).
                le(StringUtils.isNotBlank(max) && new BigDecimal(max).compareTo(new BigDecimal("0")) > 0, SkuInfoEntity::getPrice, max).
                ge(StringUtils.isNotBlank(min), SkuInfoEntity::getPrice, min);

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params), lambdaQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkuListBySpuId(Long spuId) {
        return this.list(new LambdaQueryWrapper<SkuInfoEntity>().eq(SkuInfoEntity::getSpuId,spuId));
    }

    @Override
    public SkuItemVo getSkuItem(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        //1、sku基本信息的获取  pms_sku_info
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(()->{
            SkuInfoEntity skuInfo = this.getById(skuId);
            skuItemVo.setInfo(skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //2、sku的图片信息    pms_sku_images
        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImages = skuImagesService.getBySkuId(skuId);
            skuItemVo.setImages(skuImages);
        }, threadPoolExecutor);

        //3、获取spu的销售属性组合
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueDao.getBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(skuItemSaleAttrVos);
        }, threadPoolExecutor);
        //4、获取spu的介绍
        CompletableFuture<Void> spuDescFuture = infoFuture.thenAcceptAsync((res) -> {
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDesc);
        }, threadPoolExecutor);

        //5、获取spu的规格参数信息
        CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync((res) -> {
            List<SpuItemAttrGroupVo> spuItemAttrGroupVos = attrGroupDao.getBySpuIdAndCatalogId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(spuItemAttrGroupVos);
        }, threadPoolExecutor);

        //6、获取sku的秒杀信息
        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
            R skuInfoSeckill = seckillFeignService.getSkuInfoSeckill(skuId);
            if (skuInfoSeckill.getCode() == 0) {
                SeckillSkuVo data = skuInfoSeckill.getData(new TypeReference<SeckillSkuVo>() {
                });
                skuItemVo.setSeckillInfo(data);
            }
        }, threadPoolExecutor);

        CompletableFuture.allOf(imgFuture,saleAttrFuture,spuDescFuture,attrFuture,seckillFuture).get();
        return skuItemVo;
    }

}